package com.dk.logc.utils;


import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Author 林道敬
 * @Date 2024-01-09
 * @Description
 **/
public class ObjToByteUtil {

    /**
     * 对象转Byte数组
     * @param obj
     * @return
     */
    public static byte[] objToByteArray(Object obj) {
        byte[] bytes = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        ObjectOutputStream objectOutputStream = null;
        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(obj);
            objectOutputStream.flush();
            bytes = byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            System.err.println("objectToByteArray failed, " + e);
        } finally {
            if (objectOutputStream != null) {
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    System.err.println("close objectOutputStream failed, " + e);
                }
            }
            if (byteArrayOutputStream != null) {
                try {
                    byteArrayOutputStream.close();
                } catch (IOException e) {
                    System.err.println("close byteArrayOutputStream failed, " + e);
                }
            }
        }
        return bytes;
    }

    /**
     * Byte数组转对象
     * @param bytes
     * @return
     */
    public static Object byteArrayToObj(byte[] bytes) {
        Object obj = null;
        ByteArrayInputStream byteArrayInputStream = null;
        ObjectInputStream objectInputStream = null;
        try {
            byteArrayInputStream = new ByteArrayInputStream(bytes);
            objectInputStream = new ObjectInputStream(byteArrayInputStream);
            obj = objectInputStream.readObject();
        } catch (Exception e) {
            System.err.println("byteArrayToObject failed, " + e);
        } finally {
            if (byteArrayInputStream != null) {
                try {
                    byteArrayInputStream.close();
                } catch (IOException e) {
                    System.err.println("close byteArrayInputStream failed, " + e);
                }
            }
            if (objectInputStream != null) {
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    System.err.println("close objectInputStream failed, " + e);
                }
            }
        }
        return obj;
    }

    /**
     *@Description TODO  将数组转换成对象 注意参数要一一致
     *@Author QuZhangJing
     *@Date 17:54  2018/8/30
     *@Version 1.0
     *@param obj 数据数组
     *@param classType 类字节码
     */
    public static Object arrayToObject(Object[] obj,Class<?> classType) {

        Object stu1= null;
        try {
            stu1 = classType.newInstance();

            for(int i=0;i< classType.getDeclaredFields().length;i++){

                String setMethodName="set"+classType.getDeclaredFields()[i].getName().substring(0,1).toUpperCase()+classType.getDeclaredFields()[i].getName().substring(1);

                Method setMethod=classType.getDeclaredMethod(setMethodName, new Class[]{classType.getDeclaredFields()[i].getType()});

                setMethod.invoke(stu1,obj[i]);
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return stu1;
    }
}
